public  class  AASQuoteController{

	// ****** Private fields
	// The controller
    ApexPages.StandardController stdController;

    // A set of product codes linked to the opprotunity     
    private Set<string> productCode {get;set;}
    
    // a Set of product families associated to the opportunity
    private Set<String> productFamily {get;set;}
    
    // Public properties
    
    // The quote the worksheet is a part of
    public Quote quote {get;set;}
    
    // Are we in Edit mode
    public boolean inEditMode {get;set;}

	// Are we in View mode
    public boolean getInViewMode() { return !inEditMode;}
    
    // Switch the page to Edit mode
    public PageReference editWorksheet(){ 
        inEditMode = true; 
        return null;
        }
       
    // Save the worksheet
    public PageReference SaveWorksheet(){ 
    	system.debug(stdController.getRecord()); // in order to avoid "there is no record to save" error message, get the record
        stdController.Save();
        inEditMode = false;
        
        /* save any attachments on the worksheets */
        List<String> wsFileDescList = new List<String>{MOBILE_WS_ATTACHMENT_DESC, ONLINE_WS_ATTACHMENT_DESC};
        
        for(String wsf : wsFileDescList)
        {
           wsFileCreation(wsf);
        }
        
        return null;
        }
        
    // Cancel the worksheet changes from Edit mode and return to View mode
    public PageReference CancelSaveWorksheet(){ 
        inEditMode = false;
        return null;
        }
            
    // ****** Public methods to determine which Worksheet sections to show
    public boolean getShowIYPBiddedPayPerCallWorksheet(){
        return productCode.contains('ING') || productCode.contains('YPING');
    }

    public boolean getShowIYPFixedPayPerCallWorksheet(){
        return productCode.contains('YPCALL') || productCode.contains('YPPPC');
    }

    public boolean getShowIYPBannerWorksheet(){
        return false; // productCode.contains('YPBAN');
    }

    public boolean getShowIYPPayPerClick(){
        return productCode.contains('YPCLK') || productCode.contains('YPCPC');
    }

    public boolean getShowIYPSubscription(){
        return productCode.contains('YPSUB');
    }

    public boolean getShowATTJumptabBanner(){
        return productCode.contains('JTBAN');
    }

    public boolean getshowATTJumptabSweepstakes(){
        return productCode.contains('JTSWP');
    }

    public boolean getshowMobileApplications(){
        return productCode.contains('MOBAPP'); // || productCode.contains('MOBAP');
    }

    public boolean getshowPlusmoMobileBanner(){
        return productCode.contains('PLSBAN');
    }
    
    public boolean getShowPrintPayPerCall(){
        return productCode.contains('PPPC') || productCode.contains('PRPPC');
    }
    
    public boolean getshowPrintSubscription(){
        return productCode.contains('PSUB') || productCode.contains('PRSUB');
    }

    public boolean getshowRentrak(){
    	/* no worksheet, updated 10/25/2011, AD-24156 */
    	return false;
        //return productCode.contains('RNTK') || productCode.contains('TVRSC');
    }
    
    public boolean getshowSearchExtension(){
        return productCode.contains('SCHEX');
    }    
    
    public boolean getshowUverse(){
    	/* no worksheet, updated 10/25/2011, AD-24156 */
    	return false;
        //return productCode.contains('UVRS') || productCode.contains('UVGMP');
    }    

    public boolean getshowWifi(){
        return productCode.contains('WIFI');
    }    

    public boolean getshowYahoo(){
        return productCode.contains('YAHOO'); // || productCode.contains('OANST');
    }    

    public boolean getshowYPMobileBanner(){
        return productCode.contains('YPMBAN');
    }    
    
    public Boolean getShowMobileAudienceNetwork()
    {
    	Boolean isShow = false;
    	
    	for(String pc : MOBILE_WS_PC)
    	{
    		if(productCode.contains(pc))
    		{
    			isShow = true;
    			break;
    		}
    	}
    	
    	/* prepopulate section values */
    	if(isShow)
    	{
	    	Mobile_Geo_File = new Attachment();
	        Mobile_Geo_File.Description = MOBILE_WS_ATTACHMENT_DESC;
	        Mobile_Geo_File.ParentId = quote.Id;
    	}
    	
    	return isShow;
    }
    
    public Boolean getShowOnlineAudienceNetwork()
    {
    	Boolean isShow = false;
    	
    	for(String pc : ONLINE_WS_PC)
    	{
    		if(productCode.contains(pc))
    		{
    			isShow = true;
    			break;
    		}
    	}
    	
    	/* prepopulate section values */
    	if(isShow)
    	{
    		Online_Geo_File = new Attachment();
	        Online_Geo_File.Description = ONLINE_WS_ATTACHMENT_DESC;
	        Online_Geo_File.ParentId = quote.Id;
    	}
    	
    	return isShow;
    }
    
    private void wsFileCreation(String description)
    {
    	if(description == MOBILE_WS_ATTACHMENT_DESC)
    	{
    		if(Mobile_Geo_File != null && Mobile_Geo_File.Body != null)
	        {
	        	if(Mobile_Geo_File.Id == null)
	        	{
	        		Mobile_Geo_File.Name = MOBILE_WS_ATTACHMENT_DESC + ' - ' + Mobile_Geo_File.Name;
	        	    insert Mobile_Geo_File;
	        	}
	        }
    	}
    	else if(description == ONLINE_WS_ATTACHMENT_DESC)
    	{
    		if(Online_Geo_File != null && Online_Geo_File.Body != null)
	        {
	        	if(Online_Geo_File.Id == null)
	        	{
	        		Online_Geo_File.Name = ONLINE_WS_ATTACHMENT_DESC + ' - ' + Online_Geo_File.Name;
	        	    insert Online_Geo_File;
	        	}
	        }
    	}
    }
    
    public static final List<String> MOBILE_WS_PC = new List<String>{'MOBSA','MOBTC','MANST','MOBCO','MANVI'};
    public static final List<String> ONLINE_WS_PC = new List<String>{'YPBAN','OANVI','OANST','OANAT'};

    private static final String MOBILE_WS_ATTACHMENT_DESC = 'Mobile Worksheet Attachment';
    private static final String ONLINE_WS_ATTACHMENT_DESC = 'Online Worksheet Attachment';
    
    public Attachment Mobile_Geo_File {get;set;}
    public Attachment Online_Geo_File {get;set;}

	// Constructor
    public  aasQuoteController(ApexPages.StandardController  stdController){
        inEditMode  = false;
        this.stdController = stdController;
        this.quote  =  (Quote)stdController.getRecord();
        
        // Create a list of Product codes linked to the opportunity
        List<OpportunityLineItem> oliList = [Select o.PricebookEntry.ProductCode, o.PricebookEntry.Product2.Family from OpportunityLineItem o where opportunityid = :quote.opportunityid]; // TODO: filter by ID
        productCode = new Set<string>();
        productFamily = new Set<String>();
        
        for(OpportunityLineItem oli : oliList)
        {
            productCode.add(oli.PricebookEntry.ProductCode);
            productFamily.add(oli.PricebookEntry.Product2.Family);
        }
    }

}